import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { CommService } from './../../comm/comm.service';
import { Http, Response } from '@angular/http';
import { Router } from '@angular/router';

import 'rxjs/add/operator/map';
import { Subject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';

import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import { ResponseModel } from '../../model/comm/response-model';
import { TipFaultSummary } from '../../model/report/tip-fault-summary';

declare const $: any;
import * as moment from 'moment';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDataGridComponent } from '../../../../../node_modules/devextreme-angular';
import { DxDropDownBoxComponent } from 'devextreme-angular';
declare var swal: any;

@Component({
    selector: 'app-config-tip-status',
    templateUrl: './config-tip-status.component.html',
    styleUrls: ['./config-tip-status.component.css'],
})

export class ConfigTipStatusComponent implements OnInit, AfterViewInit {

    @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;
    /**查询按类型 */
    public typeItems = [{ id: 1, text: '按值统计' }, { id: 2, text: '按班次统计' }];
    public typeSelected = this.typeItems[0];

    /**选择项 */
    public items: TipFaultSummary[] = [];
    public selectedItems: TipFaultSummary[] = [];


    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public shiftItems: any[];
    public equipemntItems: TVResource[] = [];

    public subAareSelected: any;
    public shiftSelected: any;
    public equipmentSelected: TVResource[] = [];
    constructor(private commServices: CommService) {
    }
    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        this.equipmentSelected = this.equipemntItems;
        //this.equipmentSelected = this.equipemntItems;
    }
    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD HH:mm');
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems;
    }

    ngAfterViewInit() {
    }

    /**获取表格数据*/
    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.resource);
        queryModel.shift = this.shiftSelected.shiftId;
        queryModel.updateType = this.typeSelected.id.toString();
        const url: string = ReportUrls.TipFilterUrl + '/' + this.typeSelected.id.toString();
        this.commServices
            .getList(queryModel, url)
            .subscribe(model => {
                this.items = [];
                if (model.resultCode === 200) {
                    this.items = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }
    public updateFilterList: TipFaultSummary[] = [];
    onCheckBoxChange(entity: TipFaultSummary): void {
        const index = this.items.findIndex(e => e.startTime === entity.startTime
            && e.subArea === entity.subArea && e.station === entity.station
            && e.shfitDay === entity.shfitDay && e.shiftId === entity.shiftId);
        if (index !== -1) {
            this.items[index].reportFilter = entity.reportFilter === 1 ? 0 : 1;
            const upIndex = this.updateFilterList.findIndex(e => e.startTime === entity.startTime
                && e.subArea === entity.subArea && e.station === entity.station
                && e.shfitDay === entity.shfitDay && e.shiftId === entity.shiftId);
            if (upIndex !== -1) {
                this.updateFilterList[upIndex] = this.items[index];
            } else {
                this.updateFilterList.push(this.items[index]);
            }
        }
    }
    saveChanges() {
        this.commServices.update(this.updateFilterList, ReportUrls.TipFilterUpdateUrl + '/1')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.updateFilterList = [];
                    alert('更新成功！');
                } else {
                    // console.log(model.resultCode + ',' + model.resultMessage);
                    alert('更新失败！' + model.resultMessage);
                }
            });
    }

    /**获取TimeRange的值*/
    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }
    @ViewChild("dddb") dddb: DxDropDownBoxComponent;
    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }
}
